<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class map_category extends CI_Controller {
	
	public function get_info(){
		if($this->check_session()){
			$this->load->Model("map_category_DAO");
			$category_level_1_id = $_POST['category_level_1_id'];
			$category_level_2_id = $_POST['category_level_2_id'];
			$returnData = $this->map_category_DAO->get($category_level_1_id, $category_level_2_id);
			echo json_encode($returnData);
		} else {
			$this->logout();
		}
    }
	
	public function update(){
		if($this->check_session()){
			$this->load->Model("map_category_DAO");
			$this->load->Model("model/map_category_model");
			$category_level_1_id = $_POST['category_level_1_id'];
			$category_level_2_id = $_POST['category_level_2_id'];
			$list_category_level_3 = $_POST['list_category_level_3'];
			$this->map_category_DAO->delete($category_level_1_id , $category_level_2_id);
			foreach($list_category_level_3 as $category_level_3_id){
				$map_category_model = new map_category_model();
				$map_category_model->category_level_1_id = $category_level_1_id;
				$map_category_model->category_level_2_id = $category_level_2_id;
				$map_category_model->category_level_3_id = $category_level_3_id;
				$this->map_category_DAO->add($map_category_model);
			}
		echo '1';
		} else {
			$this->logout();
		}
    }
	
	public function logout(){
		$this->session->unset_userdata('session_username');
		$this->session->unset_userdata('session_role');
		redirect('/', 'refresh');
    }
	
	public function check_session(){
		$session_username = $this->session->userdata('session_username');
		return isset($session_username) && $session_username != '';
	}
}
